package com.talkingdata.hadoop.guard.mapper;

import com.talkingdata.hadoop.guard.bean.NameNodeState;

import org.apache.ibatis.annotations.*;

import java.util.ArrayList;

@Mapper
public interface NamenodeMapper {
    @Insert("insert into name_node_state (" +
            "service_id,nn_id,address,service_state," +
            "rpc_address,http_address,timestamp) values (" +
            "#{serviceId},#{nnId},#{address},#{serviceState},#{rpcAddress},#{httpAddress},#{timestamp})")
    int insertNamenode (NameNodeState nameNodeState);

    @Select("select * from name_node_state " +
            "where service_id = #{service_id} and " +
            "nn_id = #{nn_id} order by id DESC limit 1 ")
    @Results({
            @Result(property = "serviceId" ,column = "service_id"),
            @Result(property = "nnId" ,column = "nn_id"),
            @Result(property = "serviceState" ,column = "service_state"),
            @Result(property = "rpcAddress" ,column = "rpc_address"),
            @Result(property = "httpAddress" ,column = "http_address"),
    })
    NameNodeState selectNameNodeStatebynnId(@Param("service_id") String service_id,@Param("nn_id") String nn_id);


    @Select("select * from name_node_state " +
            "where service_id = #{service_id} and " +
            "service_state = 'active' order by id DESC limit 1 ")
    @Results({
            @Result(property = "serviceId" ,column = "service_id"),
            @Result(property = "nnId" ,column = "nn_id"),
            @Result(property = "serviceState" ,column = "service_state"),
            @Result(property = "rpcAddress" ,column = "rpc_address"),
            @Result(property = "httpAddress" ,column = "http_address"),
    })
    NameNodeState selectActiveNameNodeStatebyServiceId(@Param("service_id") String service_id);


    @Select("select * from name_node_state where service_id=#{service_id} and timestamp =(select timestamp from name_node_state where service_id=#{service_id} ORDER BY id DESC LIMIT 1)")
    @Results({
            @Result(property = "serviceId" ,column = "service_id"),
            @Result(property = "nnId" ,column = "nn_id"),
            @Result(property = "serviceState" ,column = "service_state"),
            @Result(property = "rpcAddress" ,column = "rpc_address"),
            @Result(property = "httpAddress" ,column = "http_address"),
    })
    ArrayList <NameNodeState> selectNameNodeStatebyServiceId(@Param("service_id") String service_id);
}
